package com.lpc.kotlintest

import org.junit.Test
import java.util.*
import kotlin.properties.Delegates
import kotlin.reflect.KProperty

/**
 * 功能:代理属性
 *
 * 描述:
 *
 * Created by lipc0113 on 2018/7/13.
 */
class ExampleUnitTest3 {

    @Test
    fun addition_isCorrect() {
        var list: MutableList<Object> = ArrayList<Object>()
        var list2: List<String> = ArrayList<String>()
        var list3: List<Object> = ArrayList<Object>()

        val listOf = mutableListOf<Any>("1", "2")

        val listString = listOf<String>("1", "2")

        listOf.addAll(listString)
//        list.addAll(list2) //error
    }

    @Test
    fun addition_isCorrect2() {

        var t = Test3()
        t.v = 123
        print(t.v)
    }

    @Test
    fun addition_isCorrect3() {

        val user = User()
        user.name2 = "first"
        user.name2 = "second"
    }

    class User {
        var name2: String by Delegates.observable("111") {

            property, old, new ->
            println("${property.name} $old -> $new")
        }
    }

    class Foo {
        var bar: String by Delegates.notNull() //错误必须初始化
    }


    //代理属性
    class Test3 {
        var v: Int by Test4()

//        var vv :Int by Delegates.lazy
    }

    class Test4 {
        operator fun getValue(test3: Test3, property: KProperty<*>): Int {

            println("111 ${property.name}")
            return 111
        }

        operator fun setValue(test3: Test3, property: KProperty<*>, i: Int) {

            println("2222 ${property.name}")
        }

    }
}